/**
 * @author Krzysztof Rozalski
 */

package hapi.sprite.blit.canvas;

import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.DisplayObjectContainer;
import flash.events.Event;
import hapi.sprite.blit.BlitBitmapSpriteSheetDisplay;
import hapi.sprite.display.Displayable;
import haxe.FastList;
import haxe.Timer;

class BitmapBlitCanvas extends BlitCanvasBase, implements Displayable {

	var displays:List<BlitBitmapSpriteSheetDisplay>;
	public var bitmap(default, null):Bitmap;
	
	public function new(width:Int, height:Int, ?frameRate:Null<Float>) {
		super(width, height, frameRate);
		this.displays = new List<BlitBitmapSpriteSheetDisplay>();
		this.bitmap = new Bitmap(new BitmapData(width, height)); 
	}
	
	public override function refresh():Void {
		var bd = bitmap.bitmapData;
		bd.lock();
		for ( d in displays ) d.renderOn(bitmap); 
		bd.unlock();
		super.refresh();
	}
	
	public function add(display:BlitBitmapSpriteSheetDisplay) {
		displays.add(display);
	}
	
	public function remove(display:BlitBitmapSpriteSheetDisplay) {
		displays.remove(display);
	}
	
	public function addToDisplayList(root:DisplayObjectContainer, ?x:Null<Float>, ?y:Null<Float>):Void {
		root.addChild(bitmap);
		if (x != null ) bitmap.x = x;
		if (y != null ) bitmap.y = y;
	}
}